<template>
	<div id="app">
		<!--应用程序栏-->
		<v-app-bar app color="#FF5252">
			<v-list-item-title style="text-align: center;font-size:18px;color:white">我的房屋</v-list-item-title>
			<v-btn elevation="0" small color="#FF5252" router-link :to="{ path: '/main/mine' }" class="go_back">
				<icon name="back" scale="2" color="white"></icon>
			</v-btn>
		</v-app-bar>
		<!-- 如果记录是空的就展示该模块 -->
		<div class="no_content" v-if="is_null">
			<icon name="no_content" scale="15"></icon>
		</div>
		
		<div v-if="is_content">
			<div style="padding-top: 2%;width: 95%;margin: 0 auto;">
				<icon name="warn" scale="2"></icon><span style="margin-left: 3%;">您当前共有{{house_list.house_count}}套房产正在出租！</span>
			</div>
			<v-card class="mx-auto" width="100%" style="margin-top: 1%;" outlined v-for="(house,i) in house_list.house_list"
				:key="i">
				<v-list-item three-line @click="goHouseDel(house.house_id)">
					<v-list-item-avatar tile size="100" width="130" class="image_border_list"><img :src="house.index_image_url" alt="未加载" />
					</v-list-item-avatar>
					<v-list-item-content>
						<v-list-item-title class="headline mb-1"><span style="font-size: 16px;">{{house.title}}</span></v-list-item-title>
						<v-list-item-subtitle><span>{{house.area_name}}</span>&nbsp;&nbsp;|&nbsp;&nbsp;<span>{{house.lease_type}}</span>&nbsp;&nbsp;|&nbsp;&nbsp;<span>{{house.acreage}}m²</span>&nbsp;&nbsp;|&nbsp;&nbsp;<span>{{house.renovation}}</span></v-list-item-subtitle>
						<div class="overline" style="color:#FF5252">{{house.month_price}}元/月</div>
					</v-list-item-content>

				</v-list-item>
				<v-divider></v-divider>
				<!-- 如果刚订单的状态为已支付，则修改房屋状态判断是否已经出租 -->
				<v-card-actions style="float: right; height: 4vh;">
					<div style="margin-right: 10px ;">
						<v-btn outlined style="height: inherit;">{{house.status}}</v-btn>
					</div>
					<div style="margin-right: 10px ;" v-if="house.status=='空闲'">
						<v-btn outlined style="color:red;height: inherit;" @click="del(house.house_id)">删除</v-btn>
					</div>
				</v-card-actions>
				<div class="clearfloat"></div>
			</v-card>
		</div>
		<v-snackbar v-model="snackbar" bottom :timeout="timeout">
			{{ message }}
		</v-snackbar>
		
	</div>
</template>

<script>
	import axios from "axios";
	export default {
		name: "MyHouse",
		data() {
			return {
				house_list: [],
				is_null: false,
				is_content: false,
				snackbar:false,
				message:'',
				timeout:1000
			}
		},
		methods: {
			goHouseDel(house_id) {
				// localStorage.setItem('data', id);
				this.$router.push({
					name: "房屋详情",
					query: {
						house_id: house_id
					}
				});
			},
			del(house_id){
				let _this = this;
				axios
					.delete("/api/v1.0/users/del_my_lease",{
						params: {
							house_id:house_id
						}
					})
					.then(function(response) {
						if (response.data.errno == '0') {
							_this.snackbar = true;
							_this.message = response.data.errmsg
							setTimeout(function() {
								_this.$router.go(0)
							}, 1000);
						} else if (response.data.errno == '4101') {
							// 未登跳转到登录页面
							setTimeout(function() {
								_this.$router.replace({
									path: "/login"
								});
							}, 1000);
						}else{
							_this.snackbar = true;
							_this.message = ""+response.data.errmsg+",当前房屋存在相关请求";
						}
					})
					.catch(function() {
						_this.message = "连接服务器失败！";
					});
			}
		},
		mounted() {
			let _this = this;
			axios
				.get("/api/v1.0/users/get_my_lease")
				.then(function(response) {
					if (response.data.errno == '0') {
						_this.house_list = response.data.data;
						console.log(response.data.data);
						if (_this.house_list.house_list.length == 0) {
							_this.is_null = true;
						} else {
							_this.is_content = true;
						}
					} else if (response.data.errno == '4101') {
						// 未登跳转到登录页面
						setTimeout(function() {
							_this.$router.replace({
								path: "/login"
							});
						}, 1000);
					}
				})
				.catch(function() {
					_this.message = "连接服务器失败！";
				});

		}
	}
</script>

<style scoped>
</style>
